﻿
var Album =
{
    DeleteImagePost: "",
    SaveImageItemPost: "",
    ProductId: "",
    ItemUpdatePost: "",
    SetCoverUrl: "",
    ShowFileManage: function () {
        var ckfinder = new CKFinder(null, null, 500, "SetUrl");
        var dialog = $("<div title='文件浏览器'></div>")
        dialog.append(ckfinder.CreateHtml());
        dialog.dialog(
            { modal: true
            , width: 800
            , close: function () { $(this).dialog("destory"); dialog.remove(); }
            , closeText: "×"
            });
    },
    Init: function () {
        // 插入新图片
        $(":input[name='btnInsertNew']").click(function () {
            //插入回调
            window["SetUrl"] = function (filePath) {
                $.post(Album.SaveImageItemPost, { rnd: Math.random, filePath: filePath, ProductId: Album.ProductId }, function (result) { location.reload(); });
            }
            Album.ShowFileManage();
        });
        // 设置封面
        $(":input[name='btnSetCover']").click(function () {
            var oCheckList = $("#albumListContent :checked");
            if (oCheckList.length != 1) { alert("必须并且只能选取一张图片作为封面!"); return false; }
            $.post(Album.SetCoverUrl, { rnd: Math.random(), imageId: oCheckList.val() }, function (result) { location.reload(); });
        });
        // 点击图片修改图片
        $("#albumListContent img").click(function () {
            var imageId = $(this).parent().find(":input[name='ImageId']").val();
            var imageName = $(this).parent().find(":input[name='ImageName']").val();
            var isCover = $(this).parent().find(":checkbox").attr("checked");
            window["SetUrl"] = function (filePath) { $.post(Album.SaveImageItemPost, { isCover: isCover, imageId: imageId, imageName: imageName, rnd: Math.random, filePath: filePath, ProductId: Album.ProductId }, function () { location.reload(); }); }
            Album.ShowFileManage();
        });
        // 批量删除图片
        $(":input[name='btnDelete']").click(function () {
            //由于封面图默认是被选中的，因此需要把第一张的勾选去掉
            var allCheeck = this.checked;
            var chkList = $("#albumListContent :checked[name='isCover']");
            var vals = [];
            $.each(chkList, function (i, n) { vals[i] = n.value; });
            if (confirm("删除警告:确定要删除吗?\r\n该操将作从服务器彻底删除图片且不可恢复，请谨慎操作\r\n注意:无论你是否有勾选封面,删除图片都不会删除封面,只有在删除商品的时候才会删除封面!"))
                $.post(Album.DeleteImagePost, { rnd: Math.random(), "ImageId": vals }, function (result) { location.reload(); });

        });
        // 全选反选
        $(":checkbox[name='ckSelectAll']").click(function () {
            var allCheeck = this.checked;
            var chkList = $("#albumListContent :checkbox[name='isCover']");
            $.each(chkList, function (i, n) {
                n.checked = allCheeck;
            });
        });
        // 批量改图
        $(":input[name='btnModify']").click(function () {
            var post = $("<form action='' method='post' class='albumItemPost'></form>");
            var list = $("<ul ></ul>");
            var itemCount = 0;
            $("#albumListContent :checked").parent().parent().find("img").each(function (i, n) {
                itemCount++;
                var item = "<li" + ((i % 2) != 0 ? " class='odd'" : "") + ">";
                item += "<div class='img'>";
                item += n.outerHTML;
                item += "</div>";
                item += "<div class='desc'>";
                item += "<input type='hidden' name='imageId' value='" + $(n).parent().find(":hidden[name='ImageId']").val() + "' />";
                item += "<p><span>标题:</span><textarea name='tbTitle' rows='5' cols='50'>" + n.title + "</textarea></p>";
                item += "<p><span>排序:</span><input type='text' name='sequence' value='" + $(n).parent().find(":hidden[name='Sequence']").val() + "' /></p>";
                item += "<p><span>描述:</span><textarea name='description' rows='5' cols='50'>" + n.alt + "</textarea></p>";
                item += "</div>";
                item += "</li>";
                list.append(item);
            });
            post.append(list);
            if (itemCount > 0)
                post.dialog({
                    width: 900
                    , height: itemCount > 4 ? $(document).height() - 100 : "auto"
                    , closeText: "×"
                    , title: "描述/排序 -- 网页对话框"
                    , modal: true
                    , buttons: {
                        "保存": function () { $(this).ajaxSubmit({ url: Album.ItemUpdatePost, success: function () { location.reload(); } }); }
                    }
                    , close: function () { $(this).dialog("destroy") }
                });
        });
    }

}
